# import matplotlib.pyplot as plt
# import pandas as pd
#
# # 读取 CSV 文件
# netsense_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_NetSense.csv'
# topk_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_TopK.csv'
# allreduce_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_AllReduce.csv'
#
# netsense_data = pd.read_csv(netsense_file_path)
# topk_data = pd.read_csv(topk_file_path)
# allreduce_data = pd.read_csv(allreduce_file_path)
#
# def get_x_and_y(data):
#     relative_time = data['Time (s)']
#     accuracy = data['Accuracy']
#     return relative_time, accuracy
#
# # 获取 NetSenseML、TopK 和 AllReduce 的数据
# relative_time_netsense, accuracy_netsense = get_x_and_y(netsense_data)
# relative_time_topk, accuracy_topk = get_x_and_y(topk_data)
# relative_time_allreduce, accuracy_allreduce = get_x_and_y(allreduce_data)
#
# # 确定 NetSenseML 收敛的最大时间
# netsense_max_time = relative_time_netsense.max()
#
# # 截断 TopK 和 AllReduce 的时间点，超过 NetSenseML 最大时间的部分不绘制
# relative_time_topk_truncated = relative_time_topk[relative_time_topk <= netsense_max_time]
# accuracy_topk_truncated = accuracy_topk[:len(relative_time_topk_truncated)]
#
# relative_time_allreduce_truncated = relative_time_allreduce[relative_time_allreduce <= netsense_max_time]
# accuracy_allreduce_truncated = accuracy_allreduce[:len(relative_time_allreduce_truncated)]
#
# # 绘制 TTA 曲线
# plt.figure(figsize=(10, 6))
#
# # NetSenseML 的曲线
# pink_color = (221 / 255, 159 / 255, 221 / 255)
# plt.plot(relative_time_netsense, accuracy_netsense, label='NetSenseML', marker='o', markersize=6, linestyle='-', linewidth=2, color=pink_color)
#
# # TopK 的曲线，截断部分不再绘制
# plt.plot(relative_time_topk_truncated, accuracy_topk_truncated, label='TopK-0.1', marker='s', markersize=8, linestyle='-', linewidth=2, color=(255 / 255, 222 / 255, 74 / 255))
#
# # AllReduce 的曲线，截断部分不再绘制
# grey_color = (112 / 255, 128 / 255, 143 / 255)
# plt.plot(relative_time_allreduce_truncated, accuracy_allreduce_truncated, label='AllReduce', marker='v', markersize=8, linestyle='-', linewidth=2, color=grey_color)
#
# # 在 TopK 的终点添加 "X" 标记，用不同颜色区分
# plt.scatter(relative_time_topk_truncated.iloc[-1], accuracy_topk_truncated.iloc[-1], marker='x', color='red', s=100, label='TopK End', zorder=5)
#
# # 在 AllReduce 的终点添加 "D" 标记，用不同符号区分
# plt.scatter(relative_time_allreduce_truncated.iloc[-1], accuracy_allreduce_truncated.iloc[-1], marker='D', color='red', s=100, label='AllReduce End', zorder=5)
#
# # 添加标题和标签
# plt.title('ResNet18 at 200 Mbps bottleneck bandwidth', fontsize=30)
# plt.xlabel('Time (seconds)', fontsize=20)
# plt.ylabel('Accuracy (%)', fontsize=20)
#
# # 添加基准线
# # plt.axhline(y=0.75, color='black', linestyle='--', linewidth=3)
# # plt.text(50, 0.77, 'y=75%', fontsize=16, color='black')
#
# # 调整刻度字体大小
# plt.xticks(fontsize=20)
# plt.yticks(fontsize=20)
#
# # 显示网格和图例
# plt.grid(True, linestyle='--', linewidth=0.5)
# plt.legend(loc='lower right', fontsize=20)
#
# # 紧凑布局并显示图像
# plt.tight_layout()
# plt.savefig('200M_resnet18.pdf', format='pdf', bbox_inches='tight')
# # plt.show()
#
#
#
#
#
#
#
#
#
#
#
#
# import matplotlib.pyplot as plt
# import pandas as pd
#
# # 读取 CSV 文件
# netsense_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_NetSense.csv'
# topk_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_TopK.csv'
# allreduce_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_AllReduce.csv'
#
# netsense_data = pd.read_csv(netsense_file_path)
# topk_data = pd.read_csv(topk_file_path)
# allreduce_data = pd.read_csv(allreduce_file_path)
#
# def get_x_and_y(data):
#     relative_time = data['Time (s)']
#     accuracy = data['Accuracy']
#     return relative_time, accuracy
#
# # 获取 NetSenseML、TopK 和 AllReduce 的数据
# relative_time_netsense, accuracy_netsense = get_x_and_y(netsense_data)
# relative_time_topk, accuracy_topk = get_x_and_y(topk_data)
# relative_time_allreduce, accuracy_allreduce = get_x_and_y(allreduce_data)
#
# # 确定 NetSenseML 收敛的最大时间
# netsense_max_time = relative_time_netsense.max()
#
# # 截断 TopK 和 AllReduce 的时间点，超过 NetSenseML 最大时间的部分不绘制
# relative_time_topk_truncated = relative_time_topk[relative_time_topk <= netsense_max_time]
# accuracy_topk_truncated = accuracy_topk[:len(relative_time_topk_truncated)]
#
# relative_time_allreduce_truncated = relative_time_allreduce[relative_time_allreduce <= netsense_max_time]
# accuracy_allreduce_truncated = accuracy_allreduce[:len(relative_time_allreduce_truncated)]
#
# # 绘制 TTA 曲线
# plt.figure(figsize=(10, 6))
#
# # NetSenseML 的曲线
# pink_color = (221 / 255, 159 / 255, 221 / 255)
# plt.plot(relative_time_netsense, accuracy_netsense, label='NetSenseML', marker='o', markersize=6, linestyle='-', linewidth=2, color=pink_color)
#
# # TopK 的曲线，截断部分不再绘制
# plt.plot(relative_time_topk_truncated, accuracy_topk_truncated, label='TopK-0.1', marker='s', markersize=8, linestyle='-', linewidth=2, color=(255 / 255, 222 / 255, 74 / 255))
#
# # AllReduce 的曲线，截断部分不再绘制
# grey_color = (112 / 255, 128 / 255, 143 / 255)
# plt.plot(relative_time_allreduce_truncated, accuracy_allreduce_truncated, label='AllReduce', marker='v', markersize=8, linestyle='-', linewidth=2, color=grey_color)
#
# # 在 TopK 的终点添加 "X" 标记，用不同颜色区分
# plt.scatter(relative_time_topk_truncated.iloc[-1], accuracy_topk_truncated.iloc[-1], marker='x', color='red', s=100, label='TopK End', zorder=5)
#
# # 在 AllReduce 的终点添加 "D" 标记，用不同符号区分
# plt.scatter(relative_time_allreduce_truncated.iloc[-1], accuracy_allreduce_truncated.iloc[-1], marker='D', color='red', s=100, label='AllReduce End', zorder=5)
#
# # 添加标题和标签
# plt.title('ResNet18 at 500 Mbps bottleneck bandwidth', fontsize=30)
# plt.xlabel('Time (seconds)', fontsize=20)
# plt.ylabel('Accuracy (%)', fontsize=20)
#
# # 添加基准线
# # plt.axhline(y=0.75, color='black', linestyle='--', linewidth=3)
# # plt.text(50, 0.77, 'y=75%', fontsize=16, color='black')
#
# # 调整刻度字体大小
# plt.xticks(fontsize=20)
# plt.yticks(fontsize=20)
#
# # 显示网格和图例
# plt.grid(True, linestyle='--', linewidth=0.5)
# plt.legend(loc='lower right', fontsize=20)
#
# # 紧凑布局并显示图像
# plt.tight_layout()
# plt.savefig('500M_resnet18.pdf', format='pdf', bbox_inches='tight')
# plt.show()
#
#
#
#
#
#
#
#
#
#
#
#
#
# import matplotlib.pyplot as plt
# import pandas as pd
#
# # 读取 CSV 文件
# netsense_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_NetSense.csv'
# topk_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_TopK.csv'
# allreduce_file_path = '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_AllReduce.csv'
#
# netsense_data = pd.read_csv(netsense_file_path)
# topk_data = pd.read_csv(topk_file_path)
# allreduce_data = pd.read_csv(allreduce_file_path)
#
# def get_x_and_y(data):
#     relative_time = data['Time (s)']
#     accuracy = data['Accuracy']
#     return relative_time, accuracy
#
# # 获取 NetSenseML、TopK 和 AllReduce 的数据
# relative_time_netsense, accuracy_netsense = get_x_and_y(netsense_data)
# relative_time_topk, accuracy_topk = get_x_and_y(topk_data)
# relative_time_allreduce, accuracy_allreduce = get_x_and_y(allreduce_data)
#
# # 确定 NetSenseML 收敛的最大时间
# netsense_max_time = relative_time_netsense.max()
#
# # 截断 TopK 和 AllReduce 的时间点，超过 NetSenseML 最大时间的部分不绘制
# relative_time_topk_truncated = relative_time_topk[relative_time_topk <= netsense_max_time]
# accuracy_topk_truncated = accuracy_topk[:len(relative_time_topk_truncated)]
#
# relative_time_allreduce_truncated = relative_time_allreduce[relative_time_allreduce <= netsense_max_time]
# accuracy_allreduce_truncated = accuracy_allreduce[:len(relative_time_allreduce_truncated)]
#
# # 绘制 TTA 曲线
# plt.figure(figsize=(10, 6))
#
# # NetSenseML 的曲线
# pink_color = (221 / 255, 159 / 255, 221 / 255)
# plt.plot(relative_time_netsense, accuracy_netsense, label='NetSenseML', marker='o', markersize=6, linestyle='-', linewidth=2, color=pink_color)
#
# # TopK 的曲线，截断部分不再绘制
# plt.plot(relative_time_topk_truncated, accuracy_topk_truncated, label='TopK-0.1', marker='s', markersize=8, linestyle='-', linewidth=2, color=(255 / 255, 222 / 255, 74 / 255))
#
# # AllReduce 的曲线，截断部分不再绘制
# grey_color = (112 / 255, 128 / 255, 143 / 255)
# plt.plot(relative_time_allreduce_truncated, accuracy_allreduce_truncated, label='AllReduce', marker='v', markersize=8, linestyle='-', linewidth=2, color=grey_color)
#
# # 在 TopK 的终点添加 "X" 标记，用不同颜色区分
# plt.scatter(relative_time_topk_truncated.iloc[-1], accuracy_topk_truncated.iloc[-1], marker='x', color='red', s=100, label='TopK End', zorder=5)
#
# # 在 AllReduce 的终点添加 "D" 标记，用不同符号区分
# plt.scatter(relative_time_allreduce_truncated.iloc[-1], accuracy_allreduce_truncated.iloc[-1], marker='D', color='red', s=100, label='AllReduce End', zorder=5)
#
# # 添加标题和标签
# plt.title('ResNet18 at 800 Mbps bottleneck bandwidth', fontsize=30)
# plt.xlabel('Time (seconds)', fontsize=20)
# plt.ylabel('Accuracy (%)', fontsize=20)
#
# # 调整刻度字体大小
# plt.xticks(fontsize=20)
# plt.yticks(fontsize=20)
#
# # 显示网格和图例
# plt.grid(True, linestyle='--', linewidth=0.5)
# plt.legend(loc='lower right', fontsize=20)
#
# # 紧凑布局并显示图像
# plt.tight_layout()
# plt.savefig('800M_resnet18.pdf', format='pdf', bbox_inches='tight')
# plt.show()
#
#
#
#
#
#
#
#
#
#
#
import matplotlib.pyplot as plt
import pandas as pd


def load_data_and_plot(netsense_path, topk_path, allreduce_path, title, output_file):
    # 读取 CSV 文件
    netsense_data = pd.read_csv(netsense_path)
    topk_data = pd.read_csv(topk_path)
    allreduce_data = pd.read_csv(allreduce_path)

    def get_x_and_y(data):
        relative_time = data['Time (s)']
        accuracy = data['Accuracy']
        return relative_time, accuracy

    # 获取 NetSenseML、TopK 和 AllReduce 的数据
    relative_time_netsense, accuracy_netsense = get_x_and_y(netsense_data)
    relative_time_topk, accuracy_topk = get_x_and_y(topk_data)
    relative_time_allreduce, accuracy_allreduce = get_x_and_y(allreduce_data)

    # 确定 NetSenseML 收敛的最大时间
    netsense_max_time = relative_time_netsense.max()

    # 截断 TopK 和 AllReduce 的时间点，超过 NetSenseML 最大时间的部分不绘制
    relative_time_topk_truncated = relative_time_topk[relative_time_topk <= netsense_max_time]
    accuracy_topk_truncated = accuracy_topk[:len(relative_time_topk_truncated)]

    relative_time_allreduce_truncated = relative_time_allreduce[relative_time_allreduce <= netsense_max_time]
    accuracy_allreduce_truncated = accuracy_allreduce[:len(relative_time_allreduce_truncated)]

    # 绘制 TTA 曲线
    plt.figure(figsize=(10, 6))

    # NetSenseML 的曲线
    netsense_color = (221 / 255, 159 / 255, 221 / 255)
    plt.plot(relative_time_netsense, accuracy_netsense, label='NetSenseML', marker='o', markersize=6, linestyle='-',
             linewidth=2, color=netsense_color)

    # TopK 的曲线，截断部分不再绘制
    tok_color = (255 / 255, 222 / 255, 74 / 255)
    plt.plot(relative_time_topk_truncated, accuracy_topk_truncated, label='TopK-0.1', marker='s', markersize=8,
             linestyle='-', linewidth=2, color=tok_color)

    # AllReduce 的曲线，截断部分不再绘制
    allreduce_color = (112 / 255, 128 / 255, 143 / 255)
    plt.plot(relative_time_allreduce_truncated, accuracy_allreduce_truncated, label='AllReduce', marker='v',
             markersize=8, linestyle='-', linewidth=2, color=allreduce_color)

    # 在 TopK 的终点添加 "X" 标记
    plt.scatter(relative_time_topk_truncated.iloc[-1], accuracy_topk_truncated.iloc[-1], marker='x', color='red', s=100,
                label='TopK End', zorder=5)

    # 在 AllReduce 的终点添加 "D" 标记
    plt.scatter(relative_time_allreduce_truncated.iloc[-1], accuracy_allreduce_truncated.iloc[-1], marker='D',
                color='red', s=100, label='AllReduce End', zorder=5)

    # 添加标题和标签
    plt.title(title, fontsize=30)
    plt.xlabel('Time (seconds)', fontsize=20)
    plt.ylabel('Accuracy (%)', fontsize=20)

    # 显示网格和图例
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.grid(True, linestyle='--', linewidth=0.5)
    plt.legend(loc='lower right', fontsize=20)

    # 紧凑布局并保存图像
    plt.tight_layout()
    plt.savefig(output_file, format='pdf', bbox_inches='tight')
    plt.show()


# 调用函数进行绘制
load_data_and_plot(
    '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_NetSense.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_TopK.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/200M/TTA_AllReduce.csv',
    'ResNet18 at 200 Mbps bandwidth',
    '200M_resnet18.pdf'
)

load_data_and_plot(
    '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_NetSense.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_TopK.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/500M/TTA_AllReduce.csv',
    'ResNet18 at 500 Mbps bandwidth',
    '500M_resnet18.pdf'
)

load_data_and_plot(
    '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_NetSense.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_TopK.csv',
    '/Users/nancy/PycharmProjects/plot/results/resnet18/800M/TTA_AllReduce.csv',
    'ResNet18 at 800 Mbps bandwidth',
    '800M_resnet18.pdf'
)
